﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using MvcMovie.Models;

namespace MvcMovie.Middleware
{
    // 中间件最简单的用法
    public class TimeRecorderMiddleware
    {
        RequestDelegate _next;

        public TimeRecorderMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context,MvcMovieContext mvcMovieContext)
        {
            //处理前执行
            var sw = new Stopwatch();
            sw.Start();


            await _next(context);
            //返回后执行
            var newDiv = @"<div id=""process"">利用中间件反馈-页面处理时间：{0} 毫秒</div></body>";
            var text = string.Format(newDiv, sw.ElapsedMilliseconds);
            await context.Response.WriteAsync(text);
        }
    }
}
